﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Gsn.Base;
using Gsn.Base.Notification.Calendar;

namespace Gsn.UI
{
    public class ConsoleUI : ICalendarUI
    {
        ICalendarController calendarController_;

        public ConsoleUI(ICalendarController calendarController)
        {
            this.CalendarController = calendarController;
        }

        #region IUI Members

        public IController Controller
        {
            get
            {
                return calendarController_;
            }
        }

        public bool Visible
        {
            get
            {
                return true;
            }
            set
            {
            }
        }

        #endregion

        #region ICalendarUI Members

        public ICalendarController CalendarController
        {
            get
            {
                return calendarController_;
            }
            set
            {
                if (calendarController_ != null)
                {
                    calendarController_.AlarmCalendarNotification -= this.DisplayAlarmCalendarNotification;
                    calendarController_.UpdateAlarmCalendarNotification -= this.DisplayInformativeCalendarNotification;
                    calendarController_.InformativeCalendarNotification -= this.DisplayInformativeCalendarNotification;
                }

                calendarController_ = value;

                if (calendarController_ != null)
                {
                    calendarController_.AlarmCalendarNotification += this.DisplayAlarmCalendarNotification;
                    calendarController_.UpdateAlarmCalendarNotification += this.DisplayInformativeCalendarNotification;
                    calendarController_.InformativeCalendarNotification += this.DisplayInformativeCalendarNotification;
                }
            }
        }

        #endregion

        private void DisplayAlarmCalendarNotification(ICalendarEngine source, CalendarNotification calendarNotification)
        {
            if (calendarNotification.Count == 0)
                return;

            Console.WriteLine("********************************************");
            Console.WriteLine("This is a calendar event alarm!!!");
            Console.WriteLine("Current time: " + DateTime.Now);

            foreach (CalendarNotificationElement calendarNotificationElement in calendarNotification)
            {
                Console.WriteLine();
                Console.WriteLine("Event: " + calendarNotificationElement.Title);
                Console.WriteLine("Start time: " + calendarNotificationElement.StartDateTime);
                Console.WriteLine("End time: " + calendarNotificationElement.EndDateTime);
                Console.WriteLine("Description (if any): " + calendarNotificationElement.Description);
            }

            Console.WriteLine("********************************************");
        }

        private void DisplayInformativeCalendarNotification(ICalendarEngine source, CalendarNotification calendarNotification)
        {
            if (calendarNotification.Count == 0)
                return;

            Console.WriteLine("********************************************");
            Console.WriteLine(calendarNotification.Title);
            Console.WriteLine(calendarNotification.Created);

            foreach (CalendarNotificationElement calendarNotificationElement in calendarNotification)
            {
                Console.WriteLine();
                Console.WriteLine("Event: " + calendarNotificationElement.Title);
                Console.WriteLine("Start time: " + calendarNotificationElement.StartDateTime);
                Console.WriteLine("End time: " + calendarNotificationElement.EndDateTime);
                Console.WriteLine("Description (if any): " + calendarNotificationElement.Description);
            }

            Console.WriteLine("********************************************");
        }
    }
}
